
using System;
using System.Text;
using SpotifyGenNHibernate.CEN;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Exceptions;
using SpotifyGenNHibernate.EN.Spotify;
using System.Collections.Generic;

namespace SpotifyGenNHibernate.CAD.Spotify
{
        public partial class AlbumCAD : BasicCAD, IAlbumCAD
        {
            public AlbumCAD() : base ()
            {
            }

            public AlbumCAD(ISession sessionAux) : base (sessionAux)
            {
            }



            public AlbumEN ReadOIDDefault (int IdAlbum)
            {
                    AlbumEN albumEN = null;

                    try
                    {
                            SesssionInitializeTransaction ();
                            albumEN = (AlbumEN)session.Load (typeof(AlbumEN),
                                    IdAlbum

                                    );
                            SessionCommit ();
                    }

                    catch (Exception ex) {
                            SessionRollBack ();
                            if (ex is ModelException)
                                    throw ex;
                            throw new DataLayerException ("Error in AlbumCAD.", ex);
                    }


                    finally
                    {
                            SessionClose ();
                    }

                    return albumEN;
            }


            public int CrearAlbum (AlbumEN album)
            {
                    try
                    {
                            SesssionInitializeTransaction ();
                            if (album.Cancion != null) {
                                    foreach (SpotifyGenNHibernate.EN.Spotify.CancionEN item in album.Cancion)
                                            session.Save (item);
                            }
                            if (album.Artista != null) {
                                    album.Artista = (SpotifyGenNHibernate.EN.Spotify.ArtistaEN)session.Load (typeof(SpotifyGenNHibernate.EN.Spotify.ArtistaEN), album.Artista.IdArtista);
                            }

                            session.Save (album);
                            SessionCommit ();
                    }

                    catch (Exception ex) {
                            SessionRollBack ();
                            if (ex is ModelException)
                                    throw ex;
                            throw new DataLayerException ("Error in AlbumCAD.", ex);
                    }


                    finally
                    {
                            SessionClose ();
                    }

                    return album.IdAlbum;
            }

            public void BorrarAlbum (int IdAlbum)
            {
                    try
                    {
                            SesssionInitializeTransaction ();
                            AlbumEN albumEN = (AlbumEN)session.Load (typeof(AlbumEN),
                                    IdAlbum

                                    );
                            session.Delete (albumEN);
                            SessionCommit ();
                    }

                    catch (Exception ex) {
                            SessionRollBack ();
                            if (ex is ModelException)
                                    throw ex;
                            throw new DataLayerException ("Error in AlbumCAD.", ex);
                    }


                    finally
                    {
                            SessionClose ();
                    }
            }

            public AlbumEN ObtenerDatos (int IdAlbum)
            {
                    AlbumEN albumEN = null;

                    try
                    {
                            SesssionInitializeTransaction ();
                            albumEN = (AlbumEN)session.Load (typeof(AlbumEN),
                                    IdAlbum

                                    );
                            SessionCommit ();
                    }

                    catch (Exception ex) {
                            SessionRollBack ();
                            if (ex is ModelException)
                                    throw ex;
                            throw new DataLayerException ("Error in AlbumCAD.", ex);
                    }


                    finally
                    {
                            SessionClose ();
                    }

                    return albumEN;
            }

            public void ModificarDatos (AlbumEN album)
            {
                    try
                    {
                            SesssionInitializeTransaction ();
                            AlbumEN albumEN = (AlbumEN)session.Load (typeof(AlbumEN), album.IdAlbum);

                            albumEN.NumeroPistas = album.NumeroPistas;


                            albumEN.Año = album.Año;


                            albumEN.Discografia = album.Discografia;

                            session.Update (albumEN);
                            SessionCommit ();
                    }

                    catch (Exception ex) {
                            SessionRollBack ();
                            if (ex is ModelException)
                                    throw ex;
                            throw new DataLayerException ("Error in AlbumCAD.", ex);
                    }


                    finally
                    {
                            SessionClose ();
                    }
            }
        }
}
